<?php
/**
 * 配置管理类
 *
 */
class search_conf
{
	protected $table;
	protected $db;
	
	function search_conf()
	{
		global $db;
		$this->table = DB_PRE . 'search_conf';
		$this->db = $db;
	}
	
	/**
	 * 添加配置
	 *
	 * @param array $data
	 * @return bool
	 */
	function add($data)
	{
		$params = $data;
//		$params['lasttime'] = 0;
		$params['otherinfo'] = $this->encode_otherinfo($data['otherinfo']);
		$res = $this->db->insert($this->table,$params);
		return $res;
	}
	
	/**
	 * 修改配置
	 *
	 * @param int $id
	 * @param array $data
	 * @return bool
	 */
	function edit($id,$data)
	{
		$params = $data;
		if(isset($data['otherinfo']))
		{
			$params['otherinfo'] = $this->encode_otherinfo($data['otherinfo']);
		}
		$res = $this->db->simple_update($this->table,$params,array('scid'=>$id));
		return $res;
	}
	
	/**
	 * 删除单个配置
	 *
	 * @param int $id
	 * @return bool
	 */
	function del($id)
	{
        return $this->db->fake_delete($this->table, array('scid'=>$id));
	}
	
	/**
	 * 根据id获得配置
	 *
	 * @param int $id
	 * @return bool
	 */
	function get_by_id($id)
	{
		$this->db->prepare('select * from '.$this->table.' where status=? and scid=?');
		$this->db->set_int(STATUS_NORMAL);
		$this->db->set_string($id);
		$info = $this->db->execute(true);
		if(!isset($info['scid']))
		{
			return false;
		}
		$info['otherinfo'] = $this->formate_otherinfo($info['otherinfo']);
//		$info['lasttime'] = date('Y-m-d H:i:s',$info['lasttime']);
		return $info;
	}
	
	/**
	 * 获得所有配置
	 *
	 * @return array
	 */
	function get_all()
	{
		$res = $this->db->query('select scid,scname,scsql,lasttime,status from '.$this->table.' where status='.STATUS_NORMAL);
		$list = array();
		if(is_array($res))
		{
			foreach ($res as $r)
			{
				$tmp = $r;
//				$tmp['lasttime'] = date('Y-m-d H:i:s', $r['lasttime']);
				$list[$r['scid']] = $tmp;
			}
		}
		return $list;
	}
	
	/**
	 * 格式化参数
	 *
	 * @param varchar $otherinfo
	 * @return array
	 */
	function formate_otherinfo($otherinfo)
	{
		$params = explode('|',$otherinfo);
		$list = array();
		if(is_array($params))
		{
			foreach ($params as $p)
			{
				$tmp = explode(':', $p);
				if(is_array($tmp) && sizeof($tmp) ==2)
				{
					$list[$tmp[0]] = $tmp[1];
				}
			}
		}
		return $list;
	}
	
	/**
	 * 对otherinfo字段编码
	 *
	 * @param array $otherinfo
	 * @return varchar
	 */
	function encode_otherinfo($otherinfo)
	{
		$str = '';
		if(is_array($otherinfo))
		{
			$tmp = array();
			foreach ($otherinfo as $k=>$v)
			{
				$tmp[] = $k.':'.$v;
			}
			$str = implode("|",$tmp);
		}
		return $str;
		
	}
}

/**
 * 数据源管理
 *
 */
class search extends search_conf 
{
	private $stable;
	public $pages;
	
	function search()
	{
		$this->stable = DB_PRE . 'search';
		$this->search_conf();
	}
	
	/**
	 * 获得数据源列表
	 *
	 * @param int $page
	 * @param int $pagesize
	 * @param int $source
	 * @param varchar $url
	 * @param int $fid
	 * @return array
	 */
	function get_search_list($page,$pagesize,$source,$url,$fid)
	{
		$start = ($page - 1) * $pagesize;
		$search_params = array(
			'status=?'=>array('set_int',STATUS_NORMAL)
		);
		if(!empty($source))
		{
			$search_params['source=?'] = array('set_string',$source);
		}
		if(!empty($url))
		{
			$search_params['url like ?'] = array('set_string','%'.$url.'%');
		}
		if(!empty($fid))
		{
			$search_params['fid=?'] = array('set_string',$fid);
		}
		$columns = array_keys($search_params);
		
		$this->db->prepare('select count(*) as nums from '.$this->stable.' where ' . implode(' and ', $columns));
		foreach ($search_params as $tmp)
		{
			$func = $tmp[0];
			$this->db->$func($tmp[1]);
		}
		$c_res = $this->db->execute(true);
		$this->db->prepare('select id,title,url,addtime,lasttime from '.$this->stable.' where ' . implode(' and ', $columns) . ' order by id desc limit ?,?');
		foreach ($search_params as $tmp)
		{
			$func = $tmp[0];
			$this->db->$func($tmp[1]);
		}
		$this->db->set_int($start);
		$this->db->set_int($pagesize);
		$res = $this->db->execute();
		
		$cout = $c_res['nums'];
		$this->pages = pages($cout,$page,$pagesize);
		return $res;
	}
	
	/**
	 * 根据id获得一条数据
	 *
	 * @param int $id
	 * @return array
	 */
	function get_data_by_id($id)
	{
		$this->db->prepare('select * from '.$this->stable.' where id=? and status='.STATUS_NORMAL);
		$this->db->set_string($id);
		$info = $this->db->execute(true);
		return $info;
	}
	
	function edit_data($id,$data)
	{
		return $this->db->simple_update($this->stable,$data,array('id'=>$id));
	}
}
?>